{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-17T13:30:54.419889Z",
     "start_time": "2020-11-17T13:30:53.715149Z"
    }
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "np.set_printoptions(precision=2)\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "from scipy import stats\n",
    "from collections import Counter\n",
    "\n",
    "sns.set_style('ticks')\n",
    "\n",
    "%matplotlib inline\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "import matplotlib as mpl\n",
    "mpl.rcParams['figure.dpi']= 300\n",
    "mpl.rc(\"savefig\", dpi=300)\n",
    "\n",
    "from scipy.special import xlogy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Read files and select drugs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-17T13:30:54.425423Z",
     "start_time": "2020-11-17T13:30:54.422495Z"
    }
   },
   "outputs": [],
   "source": [
    "ref_type = 'log2_median_ic50_hn' # log2_median_ic50_3f_hn | log2_median_ic50_hn\n",
    "model_name = 'RWEN' \n",
    "\n",
    "dosage_shifted = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-17T13:30:54.430185Z",
     "start_time": "2020-11-17T13:30:54.427468Z"
    }
   },
   "outputs": [],
   "source": [
    "norm_type = 'patient_TPM'\n",
    "\n",
    "current_dir = '../result/HN_model/{}/'.format(norm_type)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-17T13:30:54.462278Z",
     "start_time": "2020-11-17T13:30:54.432102Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Drug Name</th>\n",
       "      <th>Synonyms</th>\n",
       "      <th>Target</th>\n",
       "      <th>Target Pathway</th>\n",
       "      <th>Selleckchem Cat#</th>\n",
       "      <th>CAS number</th>\n",
       "      <th>PubCHEM</th>\n",
       "      <th>Others</th>\n",
       "      <th>entropy</th>\n",
       "      <th>max_conc</th>\n",
       "      <th>...</th>\n",
       "      <th>median_ic50_9f</th>\n",
       "      <th>log2_median_ic50_9f</th>\n",
       "      <th>log2_median_ic50_hn</th>\n",
       "      <th>median_ic50_hn</th>\n",
       "      <th>median_ic50_3f_hn</th>\n",
       "      <th>log2_median_ic50_3f_hn</th>\n",
       "      <th>median_ic50_9f_hn</th>\n",
       "      <th>log2_median_ic50_9f_hn</th>\n",
       "      <th>num_sensitive</th>\n",
       "      <th>num_sensitive_hn</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Drug ID</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1001</th>\n",
       "      <td>AICA Ribonucleotide</td>\n",
       "      <td>AICAR, N1-(b-D-Ribofuranosyl)-5-aminoimidazole...</td>\n",
       "      <td>AMPK agonist</td>\n",
       "      <td>Metabolism</td>\n",
       "      <td>S1802</td>\n",
       "      <td>2627-69-2</td>\n",
       "      <td>65110</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6.034272</td>\n",
       "      <td>2000.0000</td>\n",
       "      <td>...</td>\n",
       "      <td>206.748380</td>\n",
       "      <td>7.691732</td>\n",
       "      <td>9.939784</td>\n",
       "      <td>982.139588</td>\n",
       "      <td>327.379863</td>\n",
       "      <td>8.354822</td>\n",
       "      <td>109.126621</td>\n",
       "      <td>6.769859</td>\n",
       "      <td>476</td>\n",
       "      <td>27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1003</th>\n",
       "      <td>Camptothecin</td>\n",
       "      <td>7-Ethyl-10-Hydroxy-Camptothecin, SN-38, Irinot...</td>\n",
       "      <td>TOP1</td>\n",
       "      <td>DNA replication</td>\n",
       "      <td>S1288</td>\n",
       "      <td>7689-03-4</td>\n",
       "      <td>104842</td>\n",
       "      <td>(SN-38, S4908, 86639-52-3) (Irinotecan, S1198,...</td>\n",
       "      <td>4.609530</td>\n",
       "      <td>0.1000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.002003</td>\n",
       "      <td>-8.963413</td>\n",
       "      <td>-7.587491</td>\n",
       "      <td>0.005199</td>\n",
       "      <td>0.001733</td>\n",
       "      <td>-9.172454</td>\n",
       "      <td>0.000578</td>\n",
       "      <td>-10.757416</td>\n",
       "      <td>688</td>\n",
       "      <td>30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1004</th>\n",
       "      <td>Vinblastine</td>\n",
       "      <td>Velban</td>\n",
       "      <td>Microtubule destabiliser</td>\n",
       "      <td>Mitosis</td>\n",
       "      <td>S1248</td>\n",
       "      <td>143-67-9</td>\n",
       "      <td>6710780</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.297122</td>\n",
       "      <td>0.1000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.001599</td>\n",
       "      <td>-9.289051</td>\n",
       "      <td>-7.150982</td>\n",
       "      <td>0.007036</td>\n",
       "      <td>0.002345</td>\n",
       "      <td>-8.735945</td>\n",
       "      <td>0.000782</td>\n",
       "      <td>-10.320907</td>\n",
       "      <td>753</td>\n",
       "      <td>33</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1006</th>\n",
       "      <td>Cytarabine</td>\n",
       "      <td>Ara-Cytidine, Arabinosyl Cytosine, U-19920</td>\n",
       "      <td>Antimetabolite</td>\n",
       "      <td>DNA replication</td>\n",
       "      <td>S1648</td>\n",
       "      <td>147-94-4</td>\n",
       "      <td>6253</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6.646594</td>\n",
       "      <td>2.0000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.163032</td>\n",
       "      <td>-2.616771</td>\n",
       "      <td>-1.342632</td>\n",
       "      <td>0.394301</td>\n",
       "      <td>0.131434</td>\n",
       "      <td>-2.927594</td>\n",
       "      <td>0.043811</td>\n",
       "      <td>-4.512557</td>\n",
       "      <td>508</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1007</th>\n",
       "      <td>Docetaxel</td>\n",
       "      <td>RP-56976, Taxotere</td>\n",
       "      <td>Microtubule stabiliser</td>\n",
       "      <td>Mitosis</td>\n",
       "      <td>S1148</td>\n",
       "      <td>114977-28-5</td>\n",
       "      <td>148124</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.220984</td>\n",
       "      <td>0.0125</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000761</td>\n",
       "      <td>-10.358915</td>\n",
       "      <td>-9.792998</td>\n",
       "      <td>0.001127</td>\n",
       "      <td>0.000376</td>\n",
       "      <td>-11.377960</td>\n",
       "      <td>0.000125</td>\n",
       "      <td>-12.962923</td>\n",
       "      <td>584</td>\n",
       "      <td>32</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 27 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                   Drug Name  \\\n",
       "Drug ID                        \n",
       "1001     AICA Ribonucleotide   \n",
       "1003            Camptothecin   \n",
       "1004             Vinblastine   \n",
       "1006              Cytarabine   \n",
       "1007               Docetaxel   \n",
       "\n",
       "                                                  Synonyms  \\\n",
       "Drug ID                                                      \n",
       "1001     AICAR, N1-(b-D-Ribofuranosyl)-5-aminoimidazole...   \n",
       "1003     7-Ethyl-10-Hydroxy-Camptothecin, SN-38, Irinot...   \n",
       "1004                                                Velban   \n",
       "1006            Ara-Cytidine, Arabinosyl Cytosine, U-19920   \n",
       "1007                                    RP-56976, Taxotere   \n",
       "\n",
       "                           Target   Target Pathway Selleckchem Cat#  \\\n",
       "Drug ID                                                               \n",
       "1001                 AMPK agonist       Metabolism            S1802   \n",
       "1003                         TOP1  DNA replication            S1288   \n",
       "1004     Microtubule destabiliser          Mitosis            S1248   \n",
       "1006               Antimetabolite  DNA replication            S1648   \n",
       "1007       Microtubule stabiliser          Mitosis            S1148   \n",
       "\n",
       "          CAS number  PubCHEM  \\\n",
       "Drug ID                         \n",
       "1001       2627-69-2    65110   \n",
       "1003       7689-03-4   104842   \n",
       "1004        143-67-9  6710780   \n",
       "1006        147-94-4     6253   \n",
       "1007     114977-28-5   148124   \n",
       "\n",
       "                                                    Others   entropy  \\\n",
       "Drug ID                                                                \n",
       "1001                                                   NaN  6.034272   \n",
       "1003     (SN-38, S4908, 86639-52-3) (Irinotecan, S1198,...  4.609530   \n",
       "1004                                                   NaN  4.297122   \n",
       "1006                                                   NaN  6.646594   \n",
       "1007                                                   NaN  4.220984   \n",
       "\n",
       "          max_conc  ...  median_ic50_9f  log2_median_ic50_9f  \\\n",
       "Drug ID             ...                                        \n",
       "1001     2000.0000  ...      206.748380             7.691732   \n",
       "1003        0.1000  ...        0.002003            -8.963413   \n",
       "1004        0.1000  ...        0.001599            -9.289051   \n",
       "1006        2.0000  ...        0.163032            -2.616771   \n",
       "1007        0.0125  ...        0.000761           -10.358915   \n",
       "\n",
       "         log2_median_ic50_hn  median_ic50_hn  median_ic50_3f_hn  \\\n",
       "Drug ID                                                           \n",
       "1001                9.939784      982.139588         327.379863   \n",
       "1003               -7.587491        0.005199           0.001733   \n",
       "1004               -7.150982        0.007036           0.002345   \n",
       "1006               -1.342632        0.394301           0.131434   \n",
       "1007               -9.792998        0.001127           0.000376   \n",
       "\n",
       "         log2_median_ic50_3f_hn  median_ic50_9f_hn  log2_median_ic50_9f_hn  \\\n",
       "Drug ID                                                                      \n",
       "1001                   8.354822         109.126621                6.769859   \n",
       "1003                  -9.172454           0.000578              -10.757416   \n",
       "1004                  -8.735945           0.000782              -10.320907   \n",
       "1006                  -2.927594           0.043811               -4.512557   \n",
       "1007                 -11.377960           0.000125              -12.962923   \n",
       "\n",
       "         num_sensitive  num_sensitive_hn  \n",
       "Drug ID                                   \n",
       "1001               476                27  \n",
       "1003               688                30  \n",
       "1004               753                33  \n",
       "1006               508                25  \n",
       "1007               584                32  \n",
       "\n",
       "[5 rows x 27 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "drug_info_df = pd.read_csv('../preprocessed_data/GDSC/hn_drug_stat.csv', index_col=0)\n",
    "drug_info_df.index = drug_info_df.index.astype(str)\n",
    "\n",
    "drug_id_name_dict = dict(zip(drug_info_df.index, drug_info_df['Drug Name'].values))\n",
    "\n",
    "drug_info_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-17T13:30:54.469521Z",
     "start_time": "2020-11-17T13:30:54.464126Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Docetaxel',\n",
       " 'Doxorubicin',\n",
       " 'Epothilone B',\n",
       " 'Gefitinib',\n",
       " 'Obatoclax Mesylate',\n",
       " 'PHA-793887',\n",
       " 'PI-103',\n",
       " 'Vorinostat']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tested_drug_list = [1007, 133, 201, 1010] + [182, 301, 302] + [1012]\n",
    "[drug_id_name_dict[str(d)] for d in tested_drug_list]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-17T13:30:54.494061Z",
     "start_time": "2020-11-17T13:30:54.471627Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>patient</th>\n",
       "      <th>drug_id</th>\n",
       "      <th>kill</th>\n",
       "      <th>drug_name</th>\n",
       "      <th>cluster_p</th>\n",
       "      <th>cluster_kill</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>52.625667</td>\n",
       "      <td>Docetaxel</td>\n",
       "      <td>1</td>\n",
       "      <td>52.625667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>HN120</td>\n",
       "      <td>133</td>\n",
       "      <td>89.675468</td>\n",
       "      <td>Doxorubicin</td>\n",
       "      <td>1</td>\n",
       "      <td>89.675468</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>HN120</td>\n",
       "      <td>201</td>\n",
       "      <td>92.858719</td>\n",
       "      <td>Epothilone B</td>\n",
       "      <td>1</td>\n",
       "      <td>92.858719</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1010</td>\n",
       "      <td>22.197506</td>\n",
       "      <td>Gefitinib</td>\n",
       "      <td>1</td>\n",
       "      <td>22.197506</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>HN120</td>\n",
       "      <td>182</td>\n",
       "      <td>76.475047</td>\n",
       "      <td>Obatoclax Mesylate</td>\n",
       "      <td>1</td>\n",
       "      <td>76.475047</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  patient drug_id       kill           drug_name  cluster_p  cluster_kill\n",
       "0   HN120    1007  52.625667           Docetaxel          1     52.625667\n",
       "1   HN120     133  89.675468         Doxorubicin          1     89.675468\n",
       "2   HN120     201  92.858719        Epothilone B          1     92.858719\n",
       "3   HN120    1010  22.197506           Gefitinib          1     22.197506\n",
       "4   HN120     182  76.475047  Obatoclax Mesylate          1     76.475047"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "if dosage_shifted:\n",
    "    single_drug_pred_df = pd.read_csv(current_dir + 'pred_drug_kill_{}_{}_shifted.csv'.format(ref_type, model_name))\n",
    "else:\n",
    "    single_drug_pred_df = pd.read_csv(current_dir + 'pred_drug_kill_{}_{}.csv'.format(ref_type, model_name))\n",
    "\n",
    "\n",
    "single_drug_pred_df.loc[:, 'drug_id'] = single_drug_pred_df.loc[:, 'drug_id'].values.astype(str)\n",
    "single_drug_pred_df.loc[:, 'drug_name'] = [drug_id_name_dict[d] for d in single_drug_pred_df.loc[:, 'drug_id'].values]\n",
    "\n",
    "patient_list = sorted(list(set(single_drug_pred_df['patient'])))\n",
    "# sel_drug_id_list = sorted(list(set(single_drug_pred_df['drug_id'])))\n",
    "\n",
    "single_drug_pred_df.loc[:, 'cluster_p'] = 1\n",
    "single_drug_pred_df.loc[:, 'cluster_kill'] = single_drug_pred_df['kill']\n",
    "\n",
    "single_drug_pred_df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### List all drug pairs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-17T13:30:54.507958Z",
     "start_time": "2020-11-17T13:30:54.495788Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(168, 3)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>patient</th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>133</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>201</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>1010</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>182</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>301</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  patient     A     B\n",
       "0   HN120  1007   133\n",
       "1   HN120  1007   201\n",
       "2   HN120  1007  1010\n",
       "3   HN120  1007   182\n",
       "4   HN120  1007   301"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "drug_combi_list = []\n",
    "n_drugs = len(tested_drug_list)\n",
    "\n",
    "for p in patient_list:\n",
    "    for x in range(0, n_drugs-1):\n",
    "        for y in range(x+1, n_drugs):\n",
    "            drug_x = str(tested_drug_list[x])\n",
    "            drug_y = str(tested_drug_list[y])\n",
    "\n",
    "            drug_combi_list += [[p, drug_x, drug_y]]\n",
    "\n",
    "drug_combi_df = pd.DataFrame(drug_combi_list, columns=['patient', 'A', 'B'])\n",
    "\n",
    "print (drug_combi_df.shape)\n",
    "drug_combi_df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Get pred and info for each drug"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-17T13:30:54.525884Z",
     "start_time": "2020-11-17T13:30:54.510974Z"
    }
   },
   "outputs": [],
   "source": [
    "merge_df = pd.merge(drug_combi_df, single_drug_pred_df, how='left', left_on=['patient', 'A'], right_on=['patient', 'drug_id'])\n",
    "drug_combi_pred_df = pd.merge(merge_df, single_drug_pred_df[['patient', 'drug_id', 'drug_name', 'cluster_kill', 'kill']], how='left', left_on=['patient', 'B'], right_on=['patient', 'drug_id'], suffixes=['_A', '_B'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-17T13:30:54.543293Z",
     "start_time": "2020-11-17T13:30:54.527604Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>patient</th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>drug_id_A</th>\n",
       "      <th>kill_A</th>\n",
       "      <th>drug_name_A</th>\n",
       "      <th>cluster_p</th>\n",
       "      <th>cluster_kill_A</th>\n",
       "      <th>drug_id_B</th>\n",
       "      <th>drug_name_B</th>\n",
       "      <th>cluster_kill_B</th>\n",
       "      <th>kill_B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>133</td>\n",
       "      <td>1007</td>\n",
       "      <td>52.625667</td>\n",
       "      <td>Docetaxel</td>\n",
       "      <td>1</td>\n",
       "      <td>52.625667</td>\n",
       "      <td>133</td>\n",
       "      <td>Doxorubicin</td>\n",
       "      <td>89.675468</td>\n",
       "      <td>89.675468</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>201</td>\n",
       "      <td>1007</td>\n",
       "      <td>52.625667</td>\n",
       "      <td>Docetaxel</td>\n",
       "      <td>1</td>\n",
       "      <td>52.625667</td>\n",
       "      <td>201</td>\n",
       "      <td>Epothilone B</td>\n",
       "      <td>92.858719</td>\n",
       "      <td>92.858719</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>1010</td>\n",
       "      <td>1007</td>\n",
       "      <td>52.625667</td>\n",
       "      <td>Docetaxel</td>\n",
       "      <td>1</td>\n",
       "      <td>52.625667</td>\n",
       "      <td>1010</td>\n",
       "      <td>Gefitinib</td>\n",
       "      <td>22.197506</td>\n",
       "      <td>22.197506</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>182</td>\n",
       "      <td>1007</td>\n",
       "      <td>52.625667</td>\n",
       "      <td>Docetaxel</td>\n",
       "      <td>1</td>\n",
       "      <td>52.625667</td>\n",
       "      <td>182</td>\n",
       "      <td>Obatoclax Mesylate</td>\n",
       "      <td>76.475047</td>\n",
       "      <td>76.475047</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>301</td>\n",
       "      <td>1007</td>\n",
       "      <td>52.625667</td>\n",
       "      <td>Docetaxel</td>\n",
       "      <td>1</td>\n",
       "      <td>52.625667</td>\n",
       "      <td>301</td>\n",
       "      <td>PHA-793887</td>\n",
       "      <td>89.318748</td>\n",
       "      <td>89.318748</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  patient     A     B drug_id_A     kill_A drug_name_A  cluster_p  \\\n",
       "0   HN120  1007   133      1007  52.625667   Docetaxel          1   \n",
       "1   HN120  1007   201      1007  52.625667   Docetaxel          1   \n",
       "2   HN120  1007  1010      1007  52.625667   Docetaxel          1   \n",
       "3   HN120  1007   182      1007  52.625667   Docetaxel          1   \n",
       "4   HN120  1007   301      1007  52.625667   Docetaxel          1   \n",
       "\n",
       "   cluster_kill_A drug_id_B         drug_name_B  cluster_kill_B     kill_B  \n",
       "0       52.625667       133         Doxorubicin       89.675468  89.675468  \n",
       "1       52.625667       201        Epothilone B       92.858719  92.858719  \n",
       "2       52.625667      1010           Gefitinib       22.197506  22.197506  \n",
       "3       52.625667       182  Obatoclax Mesylate       76.475047  76.475047  \n",
       "4       52.625667       301          PHA-793887       89.318748  89.318748  "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "drug_combi_pred_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-17T13:30:54.589026Z",
     "start_time": "2020-11-17T13:30:54.545149Z"
    }
   },
   "outputs": [],
   "source": [
    "rows = []\n",
    "for _, data in drug_combi_pred_df.iterrows():\n",
    "\n",
    "    \n",
    "    cluster_kill_A = data['cluster_kill_A']\n",
    "    cluster_kill_B = data['cluster_kill_B']\n",
    "    cluster_kill_C = cluster_kill_A + cluster_kill_B - np.multiply(cluster_kill_A/100, cluster_kill_B/100)*100\n",
    "    kill_C = cluster_kill_C\n",
    "    \n",
    "    best_kill = np.max([data['kill_A'], data['kill_B']])\n",
    "    improve = kill_C - best_kill\n",
    "    improve_p = (kill_C - best_kill) / best_kill\n",
    "    \n",
    "    sum_kill_dif = np.sum(np.abs(cluster_kill_A - cluster_kill_B))\n",
    "    \n",
    "    ##### save output #####\n",
    "    \n",
    "    rows += [[kill_C, improve, improve_p, sum_kill_dif]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-17T13:30:54.612400Z",
     "start_time": "2020-11-17T13:30:54.591316Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>patient</th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>drug_id_A</th>\n",
       "      <th>kill_A</th>\n",
       "      <th>drug_name_A</th>\n",
       "      <th>cluster_p</th>\n",
       "      <th>cluster_kill_A</th>\n",
       "      <th>drug_id_B</th>\n",
       "      <th>drug_name_B</th>\n",
       "      <th>cluster_kill_B</th>\n",
       "      <th>kill_B</th>\n",
       "      <th>kill_C</th>\n",
       "      <th>improve</th>\n",
       "      <th>improve_p</th>\n",
       "      <th>sum_kill_dif</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>133</td>\n",
       "      <td>1007</td>\n",
       "      <td>52.625667</td>\n",
       "      <td>Docetaxel</td>\n",
       "      <td>1</td>\n",
       "      <td>52.625667</td>\n",
       "      <td>133</td>\n",
       "      <td>Doxorubicin</td>\n",
       "      <td>89.675468</td>\n",
       "      <td>89.675468</td>\n",
       "      <td>95.108822</td>\n",
       "      <td>5.433354</td>\n",
       "      <td>0.060589</td>\n",
       "      <td>37.049801</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>201</td>\n",
       "      <td>1007</td>\n",
       "      <td>52.625667</td>\n",
       "      <td>Docetaxel</td>\n",
       "      <td>1</td>\n",
       "      <td>52.625667</td>\n",
       "      <td>201</td>\n",
       "      <td>Epothilone B</td>\n",
       "      <td>92.858719</td>\n",
       "      <td>92.858719</td>\n",
       "      <td>96.616866</td>\n",
       "      <td>3.758147</td>\n",
       "      <td>0.040472</td>\n",
       "      <td>40.233052</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>1010</td>\n",
       "      <td>1007</td>\n",
       "      <td>52.625667</td>\n",
       "      <td>Docetaxel</td>\n",
       "      <td>1</td>\n",
       "      <td>52.625667</td>\n",
       "      <td>1010</td>\n",
       "      <td>Gefitinib</td>\n",
       "      <td>22.197506</td>\n",
       "      <td>22.197506</td>\n",
       "      <td>63.141587</td>\n",
       "      <td>10.515920</td>\n",
       "      <td>0.199825</td>\n",
       "      <td>30.428161</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>182</td>\n",
       "      <td>1007</td>\n",
       "      <td>52.625667</td>\n",
       "      <td>Docetaxel</td>\n",
       "      <td>1</td>\n",
       "      <td>52.625667</td>\n",
       "      <td>182</td>\n",
       "      <td>Obatoclax Mesylate</td>\n",
       "      <td>76.475047</td>\n",
       "      <td>76.475047</td>\n",
       "      <td>88.855211</td>\n",
       "      <td>12.380163</td>\n",
       "      <td>0.161885</td>\n",
       "      <td>23.849381</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>301</td>\n",
       "      <td>1007</td>\n",
       "      <td>52.625667</td>\n",
       "      <td>Docetaxel</td>\n",
       "      <td>1</td>\n",
       "      <td>52.625667</td>\n",
       "      <td>301</td>\n",
       "      <td>PHA-793887</td>\n",
       "      <td>89.318748</td>\n",
       "      <td>89.318748</td>\n",
       "      <td>94.939828</td>\n",
       "      <td>5.621080</td>\n",
       "      <td>0.062933</td>\n",
       "      <td>36.693081</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  patient     A     B drug_id_A     kill_A drug_name_A  cluster_p  \\\n",
       "0   HN120  1007   133      1007  52.625667   Docetaxel          1   \n",
       "1   HN120  1007   201      1007  52.625667   Docetaxel          1   \n",
       "2   HN120  1007  1010      1007  52.625667   Docetaxel          1   \n",
       "3   HN120  1007   182      1007  52.625667   Docetaxel          1   \n",
       "4   HN120  1007   301      1007  52.625667   Docetaxel          1   \n",
       "\n",
       "   cluster_kill_A drug_id_B         drug_name_B  cluster_kill_B     kill_B  \\\n",
       "0       52.625667       133         Doxorubicin       89.675468  89.675468   \n",
       "1       52.625667       201        Epothilone B       92.858719  92.858719   \n",
       "2       52.625667      1010           Gefitinib       22.197506  22.197506   \n",
       "3       52.625667       182  Obatoclax Mesylate       76.475047  76.475047   \n",
       "4       52.625667       301          PHA-793887       89.318748  89.318748   \n",
       "\n",
       "      kill_C    improve  improve_p  sum_kill_dif  \n",
       "0  95.108822   5.433354   0.060589     37.049801  \n",
       "1  96.616866   3.758147   0.040472     40.233052  \n",
       "2  63.141587  10.515920   0.199825     30.428161  \n",
       "3  88.855211  12.380163   0.161885     23.849381  \n",
       "4  94.939828   5.621080   0.062933     36.693081  "
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "drug_combi_pred_df = pd.concat([drug_combi_pred_df, pd.DataFrame(rows, columns=['kill_C', 'improve', 'improve_p', 'sum_kill_dif'])], axis=1)\n",
    "drug_combi_pred_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-17T13:30:54.632451Z",
     "start_time": "2020-11-17T13:30:54.614549Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>patient</th>\n",
       "      <th>drug_id_A</th>\n",
       "      <th>drug_name_A</th>\n",
       "      <th>drug_id_B</th>\n",
       "      <th>drug_name_B</th>\n",
       "      <th>cluster_p</th>\n",
       "      <th>cluster_kill_A</th>\n",
       "      <th>cluster_kill_B</th>\n",
       "      <th>kill_A</th>\n",
       "      <th>kill_B</th>\n",
       "      <th>kill_C</th>\n",
       "      <th>improve</th>\n",
       "      <th>improve_p</th>\n",
       "      <th>sum_kill_dif</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>Docetaxel</td>\n",
       "      <td>133</td>\n",
       "      <td>Doxorubicin</td>\n",
       "      <td>1</td>\n",
       "      <td>52.625667</td>\n",
       "      <td>89.675468</td>\n",
       "      <td>52.625667</td>\n",
       "      <td>89.675468</td>\n",
       "      <td>95.108822</td>\n",
       "      <td>5.433354</td>\n",
       "      <td>0.060589</td>\n",
       "      <td>37.049801</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>Docetaxel</td>\n",
       "      <td>201</td>\n",
       "      <td>Epothilone B</td>\n",
       "      <td>1</td>\n",
       "      <td>52.625667</td>\n",
       "      <td>92.858719</td>\n",
       "      <td>52.625667</td>\n",
       "      <td>92.858719</td>\n",
       "      <td>96.616866</td>\n",
       "      <td>3.758147</td>\n",
       "      <td>0.040472</td>\n",
       "      <td>40.233052</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>Docetaxel</td>\n",
       "      <td>1010</td>\n",
       "      <td>Gefitinib</td>\n",
       "      <td>1</td>\n",
       "      <td>52.625667</td>\n",
       "      <td>22.197506</td>\n",
       "      <td>52.625667</td>\n",
       "      <td>22.197506</td>\n",
       "      <td>63.141587</td>\n",
       "      <td>10.515920</td>\n",
       "      <td>0.199825</td>\n",
       "      <td>30.428161</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>Docetaxel</td>\n",
       "      <td>182</td>\n",
       "      <td>Obatoclax Mesylate</td>\n",
       "      <td>1</td>\n",
       "      <td>52.625667</td>\n",
       "      <td>76.475047</td>\n",
       "      <td>52.625667</td>\n",
       "      <td>76.475047</td>\n",
       "      <td>88.855211</td>\n",
       "      <td>12.380163</td>\n",
       "      <td>0.161885</td>\n",
       "      <td>23.849381</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>Docetaxel</td>\n",
       "      <td>301</td>\n",
       "      <td>PHA-793887</td>\n",
       "      <td>1</td>\n",
       "      <td>52.625667</td>\n",
       "      <td>89.318748</td>\n",
       "      <td>52.625667</td>\n",
       "      <td>89.318748</td>\n",
       "      <td>94.939828</td>\n",
       "      <td>5.621080</td>\n",
       "      <td>0.062933</td>\n",
       "      <td>36.693081</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  patient drug_id_A drug_name_A drug_id_B         drug_name_B  cluster_p  \\\n",
       "0   HN120      1007   Docetaxel       133         Doxorubicin          1   \n",
       "1   HN120      1007   Docetaxel       201        Epothilone B          1   \n",
       "2   HN120      1007   Docetaxel      1010           Gefitinib          1   \n",
       "3   HN120      1007   Docetaxel       182  Obatoclax Mesylate          1   \n",
       "4   HN120      1007   Docetaxel       301          PHA-793887          1   \n",
       "\n",
       "   cluster_kill_A  cluster_kill_B     kill_A     kill_B     kill_C    improve  \\\n",
       "0       52.625667       89.675468  52.625667  89.675468  95.108822   5.433354   \n",
       "1       52.625667       92.858719  52.625667  92.858719  96.616866   3.758147   \n",
       "2       52.625667       22.197506  52.625667  22.197506  63.141587  10.515920   \n",
       "3       52.625667       76.475047  52.625667  76.475047  88.855211  12.380163   \n",
       "4       52.625667       89.318748  52.625667  89.318748  94.939828   5.621080   \n",
       "\n",
       "   improve_p  sum_kill_dif  \n",
       "0   0.060589     37.049801  \n",
       "1   0.040472     40.233052  \n",
       "2   0.199825     30.428161  \n",
       "3   0.161885     23.849381  \n",
       "4   0.062933     36.693081  "
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "drug_combi_pred_df = drug_combi_pred_df[['patient', 'drug_id_A', 'drug_name_A', 'drug_id_B', 'drug_name_B', 'cluster_p', 'cluster_kill_A', 'cluster_kill_B', 'kill_A', 'kill_B', 'kill_C', 'improve', 'improve_p', 'sum_kill_dif']]\n",
    "\n",
    "drug_combi_pred_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-17T13:30:54.646386Z",
     "start_time": "2020-11-17T13:30:54.634443Z"
    }
   },
   "outputs": [],
   "source": [
    "if dosage_shifted:\n",
    "    drug_combi_pred_df.to_csv(current_dir + 'pred_combi_kill_{}_{}_shifted.csv'.format(ref_type, model_name), index=False)\n",
    "else:\n",
    "    drug_combi_pred_df.to_csv(current_dir + 'pred_combi_kill_{}_{}.csv'.format(ref_type, model_name), index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
